﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;

namespace YArchitech.Public
{
	public partial class DesignNoteForm : System.Windows.Forms.Form
	{
		public DesignNoteForm()
		{
			this.InitializeComponent();
			base.Icon = Resource.YArchitechIcon;
			this.GetUserSetting();
		}

		private void GetUserSetting()
		{
			DesignNoteSetParam designNoteText = new HVACUserSetting().GetDesignNoteText();
			this.SetRdoButton(designNoteText.Major);
		}

		private void SetRdoButton(int major)
		{
			switch (major)
			{
			case 0:
				this.rdoArch.Checked = true;
				return;
			case 1:
				this.rdoPlumbing.Checked = true;
				return;
			case 2:
				this.rdoHVAC.Checked = true;
				return;
			case 3:
				this.rdoElec.Checked = true;
				return;
			case 4:
				this.rdoCustom.Checked = true;
				return;
			default:
				return;
			}
		}

		private void btnPath_Click(object sender, EventArgs e)
		{
			string text = this.txtPath.Text;
			OpenFileDialog openFileDialog = new OpenFileDialog();
			openFileDialog.Filter = "rtf文件(*.rtf)|*.rtf";
			if (text != null && text.Length > 0)
			{
				openFileDialog.InitialDirectory = Path.GetDirectoryName(text);
				openFileDialog.FileName = Path.GetFileNameWithoutExtension(text);
			}
			if (openFileDialog.ShowDialog() == DialogResult.OK)
			{
				this.txtPath.Text = openFileDialog.FileName;
			}
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			this.UserSettingWrite();
			if (this.rdoCustom.Checked)
			{
				if (this.txtPath.Text == "")
				{
					YJKMessageBox.Show("请选择设计说明文档！");
					return;
				}
				if (!File.Exists(this.txtPath.Text))
				{
					YJKMessageBox.Show("设计说明文档不存在！");
					return;
				}
				this.strPath = this.txtPath.Text;
			}
			else
			{
				this.strPath = "";
				DesignNoteSetParam designNoteText = new HVACUserSetting().GetDesignNoteText();
				this.major = designNoteText.Major;
			}
			base.DialogResult = DialogResult.OK;
		}

		private void UserSettingWrite()
		{
			HVACUserSetting hvacuserSetting = new HVACUserSetting();
			DesignNoteSetParam designNoteSetParam = new DesignNoteSetParam();
			if (this.rdoArch.Checked)
			{
				designNoteSetParam.Major = 0;
			}
			if (this.rdoPlumbing.Checked)
			{
				designNoteSetParam.Major = 1;
			}
			if (this.rdoHVAC.Checked)
			{
				designNoteSetParam.Major = 2;
			}
			if (this.rdoElec.Checked)
			{
				designNoteSetParam.Major = 3;
			}
			if (this.rdoCustom.Checked)
			{
				designNoteSetParam.Major = 4;
			}
			hvacuserSetting.WriteDesignNoteText(designNoteSetParam);
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
		}

		private void DesignNoteForm_Load(object sender, EventArgs e)
		{
			this.RdoCheckedChanged();
		}

		private void RdoCheckedChanged()
		{
			if (this.rdoArch.Checked || this.rdoPlumbing.Checked || this.rdoElec.Checked || this.rdoHVAC.Checked)
			{
				this.label1.Enabled = false;
				this.txtPath.Enabled = false;
				this.btnPath.Enabled = false;
				return;
			}
			this.label1.Enabled = true;
			this.txtPath.Enabled = true;
			this.btnPath.Enabled = true;
		}

		private void rdoArch_CheckedChanged(object sender, EventArgs e)
		{
			this.RdoCheckedChanged();
		}

		private void rdoPlumbing_CheckedChanged(object sender, EventArgs e)
		{
			this.RdoCheckedChanged();
		}

		private void rdoHVAC_CheckedChanged(object sender, EventArgs e)
		{
			this.RdoCheckedChanged();
		}

		private void rdoElec_CheckedChanged(object sender, EventArgs e)
		{
			this.RdoCheckedChanged();
		}

		private void rdoCustom_CheckedChanged(object sender, EventArgs e)
		{
			this.RdoCheckedChanged();
		}

		public string strPath = "";

		public int major;
	}
}
